﻿using AutoMapper;
using Grpc.Net.Client;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using XLZF.MicroServices.IDS4;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Models;
using XLZF.MicroServices.ViewModels;

namespace XLZF.MicroServices.ClothingDictService.Controllers;

/// <summary>
/// 字典类型Api
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class ClothingClassDictController : ControllerBase
{
    private readonly IClothingClassDictRepository _clothingClassDictRepository;

    private readonly IMapper _mapper;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="clothingDictRepository"></param>
    /// <param name="mapper"></param>
    public ClothingClassDictController(IClothingClassDictRepository clothingDictRepository, IMapper mapper)
    {
        _clothingClassDictRepository = clothingDictRepository;

        _mapper = mapper;
    }


    /// <summary>
    /// 查询
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("[action]", Name = "Get")]
    public ActionResult Get()
    {
        List<Clothing_Class_Dict> list = _clothingClassDictRepository.GetLists();

        var values = _mapper.Map<List<ClothingDictViewModel>>(list);

        return Ok(values);
    }

    /// <summary>
    /// GetSearch
    /// </summary>
    /// <param name="Seachval"></param>
    /// <param name="pageIndex"></param>
    /// <param name="PageSize"></param>
    /// <returns></returns>
    [Authorize]
    [HttpGet]
    [Route("[action]", Name = "GetSearch")]
    public async Task<ActionResult> GetSearch(string? Seachval = "", int pageIndex = 1, int PageSize = 10)
    {
        Page<Clothing_Class_Dict> pagedata = _clothingClassDictRepository.GetPageData(Seachval, false, pageIndex, PageSize);

        var values = _mapper.Map<Page<Clothing_Class_Dict>, Page<ClothingDictViewModel>>(pagedata);

        //获取人员字典
        List<Person> list = await GetUser();

        var result = from a in values.LsList
                     join b in list
                     on Convert.ToInt32(a.CreateBy) equals b.Id
                     select new ClothingDictViewModel
                     {
                         ObjID = a.ObjID,
                         ObjName = a.ObjName,
                         CreateBy = b.Nickname,
                         CreateDate = a.CreateDate
                     };
        values.LsList = result.ToList();

        return Ok(values);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("[action]", Name = "Add")]
    public ActionResult Add(string Name)
    {
        var clothingclassdict = new Clothing_Class_Dict
        {
            ClassName = Name,

            Status = 1,

            CreateBy = "1",

            CreateDate = DateTime.Now
        };

        return Ok(_clothingClassDictRepository.Add(clothingclassdict));
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Name"></param>
    /// <returns></returns>
    [HttpPut]
    [Route("[action]", Name = "Update")]
    public ActionResult Update(string ID, string Name)
    {
        var obj = _clothingClassDictRepository.Get(ID);

        obj.ClassName = Name;

        obj.UpdateBy = "1";

        obj.UpdateDate = DateTime.Now;

        return Ok(_clothingClassDictRepository.Edit(obj));
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    [HttpDelete]
    [Route("[action]", Name = "Delete")]
    public ActionResult Delete(string ID)
    {
        var obj = _clothingClassDictRepository.Get(ID);

        return Ok(_clothingClassDictRepository.Del(obj));
    }

    /// <summary>
    /// gRPC 获取人员字典
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<List<Person>> GetUser()
    {
        var channel = GrpcChannel.ForAddress("https://localhost:7231");

        var client = new Users.UsersClient(channel);

        var reply = await client.GetUserModelAsync(new GetUserRequest { Userid = "1" });

        Console.WriteLine(reply.Person.ToString());

        List<Person> persons = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Person>>(reply.Person.ToString());

        return persons;
    }
}
